package com.example.bbs.service;

import com.example.bbs.bean.*;
import com.example.bbs.conf.PageConf;
import com.example.bbs.dao.*;
import com.example.bbs.http.bean.HttpSendBasic;
import com.example.bbs.http.bean.comment.CommentSend;
import com.example.bbs.http.bean.note.*;
import com.example.bbs.http.bean.type.TypeSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.Oneway;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NoteService {

    @Autowired
    private NoteMapper noteMapper;
    @Autowired
    private NotePlusMapper notePlusMapper;
    @Autowired
    private PaserService paserService;
    @Autowired
    private PageConf pageConf;
    @Autowired
    private BbstypeMapper bbstypeMapper;
    @Autowired
    private CommentPlusMapper commentPlusMapper;
    @Autowired
    private CommentMapper commentMapper;

    //首页加载数据
    public NoteIndexListSend getIndexList(Integer typeId) {
        //测试分类是否存在
        Bbstype bbstype = bbstypeMapper.selectByPrimaryKey(typeId);
        NoteIndexListSend noteIndexListSend = new NoteIndexListSend();
        if (bbstype == null) {
            //不存在异常
            noteIndexListSend.setSuccess(0);
            noteIndexListSend.setMessage("非法请求");
            return noteIndexListSend;
        }
        //查询全部置顶帖
        noteIndexListSend.setTopNotes(paserService.notePlusListTransform(notePlusMapper.byTypeTop(typeId)));
        //查询第一页全部帖子
        noteIndexListSend.setNotes(paserService.notePlusListTransform(notePlusMapper.byTypeAll(typeId, 0, pageConf.getPageSize())));
        noteIndexListSend.setSuccess(1);
        noteIndexListSend.setMessage("ok");
        //板块展示数据
        noteIndexListSend.setTypeName(bbstype.getTypename());
        noteIndexListSend.setTypeIcon(bbstype.getTypeicon());
        //查询总页长
        NoteExample noteExample = new NoteExample();
        noteExample.createCriteria().andTypeidEqualTo(typeId);
        Long count = noteMapper.countByExample(noteExample);
        noteIndexListSend.setPage(1);
        noteIndexListSend.setMaxPage((int) (count % pageConf.getPageSize() == 0 ? count / pageConf.getPageSize() : count / pageConf.getPageSize() + 1));
        return noteIndexListSend;
    }

    //分页加载
    public NoteListSend getPage(Integer typeId, Integer page) {
        //测试分类是否存在
        Bbstype bbstype = bbstypeMapper.selectByPrimaryKey(typeId);
        NoteListSend noteIndexListSend = new NoteListSend();
        if (bbstype == null) {
            noteIndexListSend.setSuccess(0);
            noteIndexListSend.setMessage("非法请求");
            return noteIndexListSend;
        }
        //加载板块指定页面
        noteIndexListSend.setNotes(paserService.notePlusListTransform(notePlusMapper.byTypeAll(typeId, (page - 1) * pageConf.getPageSize(), pageConf.getPageSize())));
        NoteExample noteExample = new NoteExample();
        noteExample.createCriteria().andTypeidEqualTo(typeId);
        //查询总页长
        Long count = noteMapper.countByExample(noteExample);
        noteIndexListSend.setPage(page);
        noteIndexListSend.setMaxPage((int) (count % pageConf.getPageSize() == 0 ? count / pageConf.getPageSize() : count / pageConf.getPageSize() + 1));
        noteIndexListSend.setSuccess(1);
        noteIndexListSend.setMessage("ok");
        return noteIndexListSend;
    }

    //帖子详情
    public NoteInit getNote(Integer noteId) {
        //根据id查询帖子
        NotePlus notePlus = notePlusMapper.byIdWithBlob(noteId);
        if (notePlus == null)
            return null;
        //帖子评论第一页
        List<CommentPlus> commentPluses = commentPlusMapper.allByNoteId(noteId, 0, pageConf.getPageSize());
        NoteAll noteAll = paserService.noteTransform(notePlus, commentPluses);
        NoteInit noteInit = new NoteInit();
        //帖子页长
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andNoteidEqualTo(noteId);
        Long count = commentMapper.countByExample(commentExample);
        noteInit.setPage(1);
        noteInit.setMaxPage((int) (count % pageConf.getPageSize() == 0 ? count / pageConf.getPageSize() : count / pageConf.getPageSize() + 1));
        noteInit.setSuccess(1);
        noteInit.setData(noteAll);
        return noteInit;
    }

    //查询评论分页
    public CommentSend commentSend(Integer noteId, Integer page) {
        //第xxx页的评论
        List<CommentPlus> commentPluses = commentPlusMapper.allByNoteId(noteId, (page - 1) * pageConf.getPageSize(), pageConf.getPageSize());
        CommentSend commentSend = new CommentSend();
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andNoteidEqualTo(noteId);
        Long count = commentMapper.countByExample(commentExample);
        commentSend.setPage(1);
        commentSend.setMaxPage((int) (count % pageConf.getPageSize() == 0 ? count / pageConf.getPageSize() : count / pageConf.getPageSize() + 1));
        commentSend.setSuccess(1);
        commentSend.setData(paserService.commentListTransform(commentPluses));
        return commentSend;
    }

    //通过人查询帖子
    public Map<String, Object> getNoteListByCreateBy(Integer createby) {
        List<Note> notes = noteMapper.selectByCreateBy(createby);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("resultCode", 1);
        resultMap.put("resultMessage", "OK");
        resultMap.put("resultData", notes);
        return resultMap;
    }

    //模糊搜索
    public NoteListSend select(String select, Integer page) {
        String key = "%%" + select.replaceAll("\\s", "%%") + "%%";
        NoteExample noteExample = new NoteExample();
        noteExample.createCriteria().andNotenameLike(key);
        List<NotePlus> noteList = notePlusMapper.select(key, (page - 1) * pageConf.getPageSize(), pageConf.getPageSize());
        NoteListSend noteListSend = new NoteListSend();
        noteListSend.setNotes(paserService.notePlusListTransform(noteList));
        Long count = noteMapper.countByExample(noteExample);
        noteListSend.setPage(1);
        noteListSend.setMaxPage((int) (count % pageConf.getPageSize() == 0 ? count / pageConf.getPageSize() : count / pageConf.getPageSize() + 1));
        noteListSend.setSuccess(1);
        return noteListSend;
    }

    public List<Note> getNoteList(){
        return noteMapper.noteList();
    }

    public List<Bbstype> selectTypeByTagid(Integer tagid){
        return noteMapper.selectTypeByTagid(tagid);
    }

    public List<Comment> commentList(Integer noteid){
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andNoteidEqualTo(noteid);
        return commentMapper.selectByExampleWithBLOBs(commentExample);
    }
}
